package com.kiteqi.wxorder.service.impl;

import com.kiteqi.wxorder.App;
import com.kiteqi.wxorder.config.UpYunConfig;
import com.kiteqi.wxorder.dto.CartDTO;
import com.kiteqi.wxorder.entity.ProductInfo;
import com.kiteqi.wxorder.enums.ProductStatusEnum;
import com.kiteqi.wxorder.enums.ResultEnum;
import com.kiteqi.wxorder.exception.SellException;
import com.kiteqi.wxorder.mapper.ProductMapper;
import com.kiteqi.wxorder.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UpYunConfig upYunConfig;

    @Override
    public int save(ProductInfo productInfo) {
        return productMapper.insert(productInfo);
    }

    @Override
    public int update(ProductInfo productInfo) {
        return productMapper.updateById(productInfo);
    }

    @Override
    public ProductInfo findOne(String productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public List<ProductInfo> findAll() {
        List<ProductInfo> productInfoList = productMapper.selectList(null);
        for (ProductInfo product : productInfoList) {
                product.addImageHost(upYunConfig.getImageHost());
        }
        return productInfoList;
    }

    @Override
    public List<ProductInfo> findUpAll() {

        List<ProductInfo> productInfoList = productMapper.findByProductStatus(ProductStatusEnum.UP.getCode());
//        List<ProductInfo> list = productMapper.findByProductStatus(App.PRODUCT_UP);
        for (ProductInfo product : productInfoList) {
            product.addImageHost(upYunConfig.getImageHost());
        }

        return productInfoList;
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO: cartDTOList) {
            ProductInfo productInfo = productMapper.selectById(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(result);

            productMapper.updateById(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO: cartDTOList) {
            ProductInfo productInfo = productMapper.selectById(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);

            productMapper.updateById(productInfo);
        }
    }

    @Override
    public int onSale(String productId) {
        ProductInfo productInfo = productMapper.selectById(productId);
        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return productMapper.updateById(productInfo);
    }

    @Override
    public int offSale(String productId) {
        ProductInfo productInfo = productMapper.selectById(productId);
        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return productMapper.updateById(productInfo);
    }
}
